package com.ziyun.erp.web.modules.supp.controller;

import com.ziyun.erp.common.api.GaoDeApi;
import com.ziyun.erp.common.utils.HttpUtils;
import com.ziyun.erp.common.utils.UserUtils;
import com.ziyun.erp.modules.supp.entity.MerchantsImageVo;
import com.ziyun.erp.modules.supp.entity.SellerInfoEntity;
import com.ziyun.erp.modules.supp.service.SellerInfoService;
import io.renren.common.utils.R;
import io.renren.common.validator.ValidatorUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.*;


/**
 *  商户入驻
 * @author gaosheng
 * @email
 * @date 2018-07-27 11:26:47
 */
@RestController
@RequestMapping("supp/sellerinfo")
public class SellerInfoController {

    @Autowired
    private SellerInfoService sellerInfoService;

    @Value("${oss.host}")
    private String ossHost;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("supp:sellerinfo:list")
    public R list(@RequestParam Map<String, Object> params){
        return sellerInfoService.queryPageSellerInfoList(params);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{sellerId}")
    @RequiresPermissions("supp:sellerinfo:info")
    public R info(@PathVariable("sellerId") Integer sellerId){
        SellerInfoEntity sellerInfo = sellerInfoService.selectById(sellerId);

        return R.ok().put("sellerInfo", sellerInfo);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("supp:sellerinfo:save")
    public R save(@RequestBody SellerInfoEntity sellerInfo){
        sellerInfoService.insert(sellerInfo);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("supp:sellerinfo:update")
    public R update(@RequestBody SellerInfoEntity sellerInfo){
        ValidatorUtils.validateEntity(sellerInfo);
        return sellerInfoService.updateInfo(sellerInfo);

    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("supp:sellerinfo:delete")
    public R delete(@RequestBody Integer[] sellerIds){
        sellerInfoService.deleteBatchIds(Arrays.asList(sellerIds));

        return R.ok();
    }

    /**
     * 商家前端状态显示
     * @param sellerId 商户ID
     * @param display 0不显示 1显示
     */
    @RequestMapping("/updateView")
    public R updateView(@RequestParam("sellerId") Integer sellerId, @RequestParam("display") String display) {
        Map map = sellerInfoService.updateView(sellerId, display);

        boolean result = (boolean) map.get("result");
        if (result){
            return R.ok();
        }else {
            return R.error(map.get("msg").toString());
        }
    }

    /**
     * 商家自提状态切换
     */
    @RequestMapping("/updatePickUp")
    public R updatePickUp(@RequestParam("sellerId") Integer sellerId, @RequestParam("pickUp") Integer pickUp){
        return sellerInfoService.updatePickUp(sellerId, pickUp);
    }

    /**
     * 录入业务员名称
     * @param sellerInfo
     * @return
     */
    @RequestMapping("/addSalesman")
    public R addSalesman(@RequestBody SellerInfoEntity sellerInfo) {
        Map map = sellerInfoService.addSalesman(sellerInfo);

        boolean result = (boolean) map.get("result");
        if (result){
            return R.ok();
        }else {
            return R.error(map.get("msg").toString());
        }
    }

    /**
     * 录入备注
     * @param sellerId 商户ID
     * @param remarks 备注
     * @return
     */
    @RequestMapping("/addRemarks")
    public R addRemarks(@RequestParam("sellerId") Integer sellerId, @RequestParam("remarks") String remarks) {
        Map resultMap = sellerInfoService.saveRemark(sellerId, remarks);
        boolean result = (boolean) resultMap.get("result");
        if (result){
            return R.ok();
        }else {
            return R.error(resultMap.get("msg").toString());
        }
    }

    /**
     *  商户审核
     * @param sellerInfo
     * @return
     */
    @RequestMapping("/audit")
    public R audit(@RequestBody SellerInfoEntity sellerInfo, HttpServletRequest request){
        String ipAddress = HttpUtils.getIPAddress(request);
        Long userId = UserUtils.getCurentUser().getUserId();
        return sellerInfoService.audit(sellerInfo, userId, ipAddress);
    }

    /**
     * 商户信息检测
     * @param sellerId
     * @return
     */
    @RequestMapping("/sellerInfoValidity")
    public R sellerInfoValidity(@RequestParam("sellerId") Integer sellerId){
        return sellerInfoService.sellerInfoValidity(sellerId);
    }

    /**
     * 查询连锁店信息
     * @param sellerId 商户ID
     * @param mainSellerId 主店ID
     * @return
     */
    @RequestMapping("/mainStore")
    public R mainStore(@RequestParam("sellerId") Integer sellerId,
                       @RequestParam("mainSellerId") Integer mainSellerId){
        List<SellerInfoEntity> list = sellerInfoService.queryMainStore(sellerId, mainSellerId);
        return R.ok().put("list", list);
    }

    /**
     * 设置总店
     * @param sellerId 商家ID
     * @param mainSellerId 主店ID
     * @return
     */
    @RequestMapping("/setMainStore")
    public R setMainStore(@RequestParam("sellerId") Integer sellerId,
                          @RequestParam("mainSellerId") Integer mainSellerId){
        Map resultMap = sellerInfoService.setMainStore(sellerId, mainSellerId);

        boolean result = (boolean) resultMap.get("result");
        if (result){
            return R.ok();
        }else {
            return R.error(resultMap.get("msg").toString());
        }
    }

    /**
     * 地址获取经纬度
     * @param addr
     * @return
     */
    @RequestMapping("/generateLongLat")
    public R generateLongLat(@RequestParam("addr") String addr){
        if (StringUtils.isBlank(addr)){
            return R.error("地址错误！");
        }
        Map map = GaoDeApi.getcodeGeo(addr);
        return R.ok().put("result", map);
    }

    /**
     * 获取自营门店列表
     */
    @RequestMapping("/selfList")
    public List<SellerInfoEntity> list(){
        Map<String, Object> param = new HashMap<>();
        param.put("self", 1);
        List<SellerInfoEntity> sellerInfoEntityList = sellerInfoService.queryParam(param);

        Iterator iter = sellerInfoEntityList.iterator();
        while (iter.hasNext()) {
            SellerInfoEntity sellerInfoEntity = (SellerInfoEntity)iter.next();
            // 剔除 中浩紫云
            if (sellerInfoEntity.getSellerId() == 1) {
                iter.remove();
            }
        }

        return sellerInfoEntityList;
    }

    /**
     * 删除商户图片信息
     */
    @RequestMapping("/removeSellerImage")
    public R removeSellerImage(@RequestParam("sellerId") Integer sellerId, @RequestParam("id") Integer id){
        return sellerInfoService.removeSellerImage(sellerId, id);
    }

    /**
     * 保存上传图片信息
     * @param merchantsImageVo
     * @return
     */
    @RequestMapping("/saveImage")
    public R saveImage(@RequestBody MerchantsImageVo merchantsImageVo){
        return sellerInfoService.saveImage(merchantsImageVo);
    }

    //冻结
    @RequestMapping("/freeze/{sellerId}")
    public R freeze(@PathVariable Integer sellerId){
        return sellerInfoService.freeze(sellerId);
    }

    //解冻
    @RequestMapping("/unfreeze/{sellerId}")
    public R unfreeze(@PathVariable Integer sellerId){
        return sellerInfoService.unfreeze(sellerId);
    }

    /**
     *  初始化商品
     */
    @RequestMapping("/copyGoods/{sellerId}")
    @RequiresPermissions("supp:sellerinfo:copyGoodsBatch")
    public R sellerCopyGoods(@PathVariable Integer sellerId){
        return sellerInfoService.sellerCopyGoods(sellerId);
    }

    /**
     * 批量初始化商品
     */
    @RequiresPermissions("supp:sellerinfo:copyGoodsBatchAll")
    @RequestMapping("/sellerCopyGoodsBatch")
    public R sellerCopyGoodsBatch(@RequestBody Integer[] sellerIds){
        return sellerInfoService.sellerCopyGoodsBatch(sellerIds);
    }

    /**
     * 批量初始化全部商品
     */
    @RequiresPermissions("supp:sellerinfo:copyGoodsBatchAll")
    @RequestMapping("/sellerCopyGoodsBatchAll")
    public R sellerCopyGoodsBatchAll(@RequestParam("nums") Integer nums){
        return sellerInfoService.sellerCopyGoodsBatchAll(nums);
    }

}
